package br.mg.bhe.ftpobre.fs;

import java.util.logging.Level;

import br.mg.bhe.ftpobre.util.LogUtil;

/**
 * 
 * @author Aureliano
 */
public final class FileNameGenerator {

	private FileNameGenerator() {
		super();
	}
	
	public static String generateFileName(String path) {
		if ((path == null) || (path.length() == 0)) {
			path = "raiz";
		}
		
		try {
			java.security.MessageDigest md = 
			             java.security.MessageDigest.getInstance("SHA1");
			md.update(path.getBytes());
			byte[] hash = md.digest();
			StringBuffer hexString = new StringBuffer();
			
			for (int i = 0; i < hash.length; i++) {
				if ((0xff & hash[i]) < 0x10)
					hexString.append(
							"0" + Integer.toHexString((0xFF & hash[i])));
				else
					hexString.append(Integer.toHexString(0xFF & hash[i]));
			}
			
			path = hexString.toString();
		} catch (Exception ex) {
			LogUtil.getLogger(FileNameGenerator.class).log(Level.SEVERE, ex.getMessage(), ex);
			throw new RuntimeException(ex);
		}
		
		return path;
	}
}